package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String,Object> resultData=new HashMap<>();
        //构造今天的日期,将时分秒毫秒都设置为0
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY,0);
        today.set(Calendar.MINUTE,0);
        today.set(Calendar.SECOND,0);
        today.set(Calendar.MILLISECOND,0);
        today.setFirstDayOfWeek(Calendar.MONDAY);//设置一周的第一天为周一

        //本周第一天
        Calendar weekFirstDay = (Calendar) today.clone();
        weekFirstDay.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
        //本月第一天
        Calendar monthFirstDay= (Calendar) today.clone();
        monthFirstDay.set(Calendar.DAY_OF_MONTH,1);
        //日期
        resultData.put("reportDate",new SimpleDateFormat("yyyy-MM-dd").format(today.getTime()));

        //会员数据
        //今日新增会员
        resultData.put("todayNewMember",memberDao.countByRegDateRange(today.getTime(),today.getTime()));
        //本周新增会员
        resultData.put("thisWeekNewMember",memberDao.countByRegDateRange(weekFirstDay.getTime(),today.getTime()));
        //本月新增会员
        resultData.put("thisMonthNewMember",memberDao.countByRegDateRange(monthFirstDay.getTime(),today.getTime()));
        //总会员
        resultData.put("totalMember",memberDao.countByRegTimeBefore(new SimpleDateFormat("yyyy-MM-dd").format(today.getTime())));

        //预约数据
        //今日预约人数
        resultData.put("todayOrderNumber",orderDao.countByOrderDateRange(today.getTime(),today.getTime(),null));
        //本周预约人数
        resultData.put("thisWeekOrderNumber",orderDao.countByOrderDateRange(weekFirstDay.getTime(),today.getTime(),null));
        //本月预约人数
        resultData.put("thisMonthOrderNumber",orderDao.countByOrderDateRange(monthFirstDay.getTime(),today.getTime(),null));

        //到诊数据
        //今日到诊人数
        resultData.put("todayVisitsNumber",orderDao.countByOrderDateRange(today.getTime(),today.getTime(), Order.ORDERSTATUS_YES));
        //本周到诊人数
        resultData.put("thisWeekVisitsNumber",orderDao.countByOrderDateRange(weekFirstDay.getTime(),today.getTime(), Order.ORDERSTATUS_YES));
        //本月到诊人数
        resultData.put("thisMonthVisitsNumber",orderDao.countByOrderDateRange(monthFirstDay.getTime(),today.getTime(), Order.ORDERSTATUS_YES));

        //热门套餐
        resultData.put("hotSetmeal",orderDao.countByHotSetmeal(4));
        return resultData;
    }
}
